﻿using Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website.ql_thi_sinh
{
    public partial class frmDiemTS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BVVDDataContext db = new BVVDDataContext();
                var listKT = (from kt in db.LST_KYTHIs
                              where kt.TRANGTHAI.Equals("1")
                              select kt).ToList();
                ddlKythi.DataSource = listKT;
                ddlKythi.DataTextField = "TENKT";
                ddlKythi.DataValueField = "MAKT";
                ddlKythi.DataBind();
                fillgrvDiemTS();
                grvDiemthiTS.DataBind();

            }
        }

        protected void ddlKythi_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            var listPT = (from pt in db.LST_PHONGTHIs
                          where pt.MAKT.Equals(ddlKythi.SelectedValue)
                          select pt).ToList();
            ddlPhongthi.DataSource = listPT;
            ddlPhongthi.DataTextField = "TENPT";
            ddlPhongthi.DataValueField = "MAPT";
            ddlPhongthi.DataBind();
            fillgrvDiemTS();
        }

        protected void ddlPhongthi_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            var listMT = (from mt in db.F_KYTHI_MONTHIs
                          where mt.MAKT.Equals(ddlKythi.SelectedValue)
                          && mt.TRANGTHAI.Equals("1")
                          select new
                          {
                              MAMH = mt.MAMH,
                              TENMH = mt.LST_MONHOC.TENMH

                          }).ToList();
            ddlMonthi.DataSource = listMT;
            ddlMonthi.DataTextField = "TENMH";
            ddlMonthi.DataValueField = "MAMH";
            ddlMonthi.DataBind();
            fillgrvDiemTS();
            grvDiemthiTS.DataBind();
        }

        protected void ddlMonthi_SelectedIndexChanged(object sender, Telerik.Web.UI.DropDownListEventArgs e)
        {
            fillgrvDiemTS();
            grvDiemthiTS.DataBind();
        }

        protected void grvDiemthiTS_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.Equals("ChamDiem"))
            {
                BVVDDataContext db = new BVVDDataContext();
                foreach (GridDataItem dr in grvDiemthiTS.Items)
                {
                    string makt = dr["MAKT"].Text;
                    string mapt = dr["MAPT"].Text;
                    string monthi = dr["MAMT"].Text;
                    string sbd = dr["SOBAODANH"].Text;
                    decimal diemthi = Convert.ToDecimal((dr.FindControl("txtDIEM") as RadNumericTextBox).Value);
                    string status = "1";
                    string ghichu = dr["GHICHU"].Text;
                    F_DIEMTHI_T obj = new F_DIEMTHI_T
                    {
                        MAKT = makt,
                        MAPT = mapt,
                        MONTHI = monthi,
                        SOBAODANH = sbd,
                        DIEM = diemthi, //String.IsNullOrEmpty(diemthi.ToString())?100:diemthi,                        
                        TRANGTHAI = status,
                        GHICHU = ghichu
                    };
                    checkRowExist(obj);


                }
                grvDiemthiTS.Rebind();
                rnAlert.Text = "Lưu điểm thành công ";
                rnAlert.Show();
            }
        }

        private string checkRowExist(F_DIEMTHI_T fc)
        {
            BVVDDataContext db = new BVVDDataContext();
            F_DIEMTHI_T value = (from ck in db.F_DIEMTHI_Ts
                               where ck.MAKT.Equals(fc.MAKT) && ck.MAPT.Equals(fc.MAPT)
                                       && ck.MONTHI.Equals(fc.MONTHI) && ck.SOBAODANH.Equals(fc.SOBAODANH)
                               select ck).SingleOrDefault();
            if (value == null)
            {
                if (fc.DIEM < 11)
                {
                    db.F_DIEMTHI_Ts.InsertOnSubmit(fc);
                    db.SubmitChanges();
                    return "Add";
                }
                else
                {
                    return "Dont Save";
                }
            }
            else if (!value.DIEM.Equals(fc.DIEM))
            {
                value.DIEM = fc.DIEM;
                
                db.SubmitChanges();
                return "Update";
            }
            else
            {

                return "Cancel";
            }

        }

        protected void grvDiemthiTS_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            fillgrvDiemTS();
        }

        protected void grvDiemthiTS_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {

        }

        private void fillgrvDiemTS()
        {
            BVVDDataContext db = new BVVDDataContext();
            string selectedKT = ddlKythi.SelectedValue;
            string selectedPT = ddlPhongthi.SelectedValue;
            string selectedMH = ddlMonthi.SelectedValue;

            var list = (from ts in db.F_PTHI_Ts
                        where ts.MAKT.Equals(selectedKT) && ts.MAPT.Equals(ddlPhongthi.SelectedValue)
                        join pt in db.LST_PHONGTHIs on ts.MAPT equals pt.MAPT into temp0
                        from tmp0 in temp0
                        join mt in db.F_KYTHI_MONTHIs on ts.MAKT equals mt.MAKT into temp1
                        from tmp1 in temp1
                        join sbd in db.F_SOBAODANHs on new { MAKT = tmp1.MAKT, SBD = ts.SOBAODANH } equals new {MAKT = sbd.MAKT, SBD = sbd.SOBAODANH } into temp2
                        from tmp2 in temp2
                        where tmp1.MAMH.Equals(ddlMonthi.SelectedValue)
                        join dt in db.F_DIEMTHI_Ts on new { MAKT = ts.MAKT, MAPT = ts.MAPT, MAMT = tmp1.MAMH,SBD= tmp2.SOBAODANH } equals new { MAKT = dt.MAKT, MAPT = dt.MAPT, MAMT = dt.MONTHI,SBD = dt.SOBAODANH } into temp3
                        from tmp3 in temp3.DefaultIfEmpty()

                        select new
                        {
                            RECID = tmp3.RECID != null ? tmp3.RECID : 0,
                            MAKT = ts.MAKT,
                            TENKT = ts.LST_KYTHI.TENKT,
                            MAPT = ts.MAPT,
                            TENPT = tmp0.TENPT,
                            TENHV = tmp2.LST_THISINH.HODEM + " " + tmp2.LST_THISINH.TENTS,
                            MAMT = tmp1.MAMH,
                            TENMT = tmp1.LST_MONHOC.TENMH,
                            SOBAODANH = tmp2.SOBAODANH,
                            DIEM = tmp3.DIEM,
                            TRANGTHAI = tmp3.TRANGTHAI,
                            GHICHU = tmp3.GHICHU

                        }).ToList();

            grvDiemthiTS.DataSource = list;



        }
    }
}